//
//  UIView+Corner.m
//  TojoyCloud
//
//  Created by rr wanggy on 2019/4/16.
//  Copyright © 2019 _Engineer_雷海洋_. All rights reserved.
//

#import "UIView+Corner.h"
#import <objc/runtime.h>
#import "UIColor+String.h"

@implementation UIView (Corner)


/// 设置圆角
/// @param corner 圆角类型
/// @param cornerRadius 圆角大小
- (void)wb_setCorner:(UIRectCorner)corner cornerRadius:(CGFloat)cornerRadius {
    CAShapeLayer *var_mask = [CAShapeLayer layer];
    UIBezierPath *var_path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(cornerRadius*2, cornerRadius*2)];
    var_mask.path = var_path.CGPath;
    self.layer.mask = var_mask;
}

/// 获取半角视图
-(void)wb_getHalfCornerView
{
    if (!self.bounds.size.width || !self.bounds.size.height) {
        return;
    }
    UIBezierPath *var_bezierPath=[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, self.wb_width, self.wb_height)];
    CAShapeLayer *var_maskLayer = [CAShapeLayer layer];
    var_maskLayer.path = var_bezierPath.CGPath;
    self.layer.mask = var_maskLayer;
}


/// 设置圆角
/// @param radiu 半径大小
-(void)wb_getCornerViewWithRadius:(CGFloat)radiu
{
    if (!self.bounds.size.width || !self.bounds.size.height) {
        return;
    }
    UIBezierPath *var_bezierPath=[UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:radiu];
    CAShapeLayer *var_maskLayer = [CAShapeLayer layer];
    var_maskLayer.path = var_bezierPath.CGPath;
    self.layer.mask = var_maskLayer;
}


/// 设置圆角
/// @param radiu 半径大小   borderColor 边框颜色  borderWidth 边框宽度
-(void)wb_getCornerViewWithRadius:(CGFloat)radiu borderColor:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth
{
    if (!self.bounds.size.width || !self.bounds.size.height) {
        return;
    }
    CAShapeLayer *var_maskLayer = nil;
    UIBezierPath *var_bezierPath=[UIBezierPath bezierPathWithRoundedRect:CGRectMake(borderWidth, borderWidth, self.bounds.size.width-borderWidth*2, self.bounds.size.height-borderWidth*2) cornerRadius:radiu];
    for (CALayer *var_subLayer in self.layer.sublayers) {
        if([var_subLayer.name isEqualToString:@"borderLayer"] && var_subLayer.superlayer){
            var_maskLayer = var_subLayer;
            break;
        }
    }
    if(!var_maskLayer){
        var_maskLayer = [CAShapeLayer layer];
        var_maskLayer.path = var_bezierPath.CGPath;
        var_maskLayer.frame = self.bounds;
        var_maskLayer.name = @"borderLayer";
    }
    var_maskLayer.strokeColor= borderColor.CGColor;
    var_maskLayer.fillColor = [UIColor clearColor].CGColor;
    var_maskLayer.lineWidth = borderWidth;
    [self.layer insertSublayer:var_maskLayer atIndex:0];
}

/// 设置顶部圆角
/// @param radiu 半径大小
-(void)wb_getTopCornerWithRatio:(CGFloat)radiu
{
    if (!self.bounds.size.width || !self.bounds.size.height) {
        return;
    }
    //得到view的遮罩路径
    UIBezierPath *var_maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(radiu,radiu)];
    //创建 layer
    CAShapeLayer *var_maskLayer = [[CAShapeLayer alloc] init];
    var_maskLayer.frame = self.bounds;
    //赋值
    var_maskLayer.path = var_maskPath.CGPath;
    self.layer.mask = var_maskLayer;

}

/// 设置底部圆角
/// @param radiu 半径大小
-(void)wb_getBottomCornerWithRatio:(CGFloat)radiu
{
    if (!self.bounds.size.width || !self.bounds.size.height) {
        return;
    }
    //得到view的遮罩路径
    UIBezierPath *var_maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(radiu,radiu)];
    //创建 layer
    CAShapeLayer *var_maskLayer = [[CAShapeLayer alloc] init];
    var_maskLayer.frame = self.bounds;
    //赋值
    var_maskLayer.path = var_maskPath.CGPath;
    self.layer.mask = var_maskLayer;
    
}

/// 设置左边圆角
/// @param radiu 半径大小
-(void)wb_getLeftCornerWithRatio:(CGFloat)radiu
{
    if (!self.bounds.size.width || !self.bounds.size.height) {
        return;
    }
    //得到view的遮罩路径
    UIBezierPath *var_maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerTopLeft cornerRadii:CGSizeMake(radiu,radiu)];
    //创建 layer
    CAShapeLayer *var_maskLayer = [[CAShapeLayer alloc] init];
    var_maskLayer.frame = self.bounds;
    //赋值
    var_maskLayer.path = var_maskPath.CGPath;
    self.layer.mask = var_maskLayer;
    
}

/// 设置边框
/// @param color 边框颜色
- (void)wb_setBorderColor:(UIColor *)color {
    [self wb_setBorderColor:color lineWidth:1];
}


/// 设置边框
/// @param color 边框颜色
/// @param lineWidth 边框大小
- (void)wb_setBorderColor:(UIColor *)color lineWidth:(CGFloat)lineWidth {
    CAShapeLayer *var_mask;
    for (CALayer *var_layer in self.layer.sublayers) {
        NSNumber *var_tag = objc_getAssociatedObject(var_layer, _cmd);
        if (var_tag.integerValue == 10000) {
            var_mask = (CAShapeLayer *)var_layer;
        }
    }
    if (!var_mask) {
        var_mask = [CAShapeLayer layer];
        objc_setAssociatedObject(var_mask, _cmd, @(10000), OBJC_ASSOCIATION_RETAIN);
        var_mask.lineWidth = lineWidth;
        //背景填充色
        var_mask.fillColor = [UIColor clearColor].CGColor;
        [self.layer addSublayer:var_mask];
    }
    UIBezierPath *var_path = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:self.frame.size.height];
    var_mask.path = var_path.CGPath;
    //圆环的颜色
    var_mask.strokeColor = color.CGColor;
}



/// layer传nil则默认为self.layer
/// @param width 宽
/// @param color 颜色
/// @param cornerRadius 半径
/// @param layer layer
- (void)wb_setupBorderWidth:(CGFloat)width color:(CGColorRef)color cornerRadius:(CGFloat)cornerRadius needMaskLayer:(CALayer *)layer
{
    for (CAGradientLayer * var_layer in self.layer.sublayers) {
        if ([var_layer isKindOfClass:CAShapeLayer.class]) {
            if (var_layer.var_Tag == 1213) {
                [var_layer removeFromSuperlayer];
                break;
            }
        }
    }
    
    CAShapeLayer *var_borderLayer = [CAShapeLayer layer];
    var_borderLayer.frame = self.bounds;
    var_borderLayer.lineWidth = width;
    var_borderLayer.var_Tag = 1213;
    var_borderLayer.strokeColor = color;
    var_borderLayer.fillColor = [UIColor clearColor].CGColor;
    
    UIBezierPath *var_bezierPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:cornerRadius];
    var_borderLayer.path = var_bezierPath.CGPath;
    
    [self.layer insertSublayer:var_borderLayer atIndex:0];
    
    CAShapeLayer *var_maskLayer = [CAShapeLayer layer];
    var_maskLayer.frame = self.bounds;
    var_maskLayer.path = var_bezierPath.CGPath;
    if (layer) {
        [self.layer setMask:var_maskLayer];
    }else{
        [layer setMask:var_maskLayer];
    }
}



@end
